<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\BaseController;
use think\Request;

class Order extends BaseController
{

    // 运费查询
    public function getFreight()
    {

        $goods_list = $this->request->param('goods_list',[]);
        $primary_address = $this->request->param('primary_address');
        $address = $this->request->param('address');
        $time = time();
        $data = [
            'time' => $time,
            "goods_list"=> $goods_list,
            "primary_address"=> $primary_address,
            "address"=> $address,
        ];
        $sign = $this->getSignature($data,config('app.APIKEY'));
        $param = [
            "api_id"=> config('app.APIID'),
            "sign"=> $sign,
            "data"=> $data,
        ];

        $url = 'http://main.miniappss.com/terminal/api.order/getFreight';

        $res = $this->curlPost($url,$param);
        if(isset($res['code']) && $res['code'] != 200){
            return $this->error([],$res['msg'],$res['error_code']);
        }
        return $this->success($res['data'],$res['msg']);
    }

    // 可售性检查
    public function checkBuy()
    {
        $goods_list = $this->request->param('goods_list');
        $primary_address = $this->request->param('primary_address');
        $address = $this->request->param('address');
        $time = time();
        $data = [
            'time' => $time,
            "goods_list"=> $goods_list,
            "primary_address"=> $primary_address,
            "address"=> $address,
        ];
        $sign = $this->getSignature($data,config('app.APIKEY'));
        $param = [
            "api_id"=> config('app.APIID'),
            "sign"=> $sign,
            "data"=> $data,
        ];

        $url = 'http://main.miniappss.com/terminal/api.order/checkBuy';

        $res = $this->curlPost($url,$param);
        if(isset($res['code']) && $res['code'] != 200){
            return $this->error([],$res['msg'],$res['error_code']);
        }
        return $this->success($res['data'],$res['msg']);
    }

    // 下单
    public function placeOrder()
    {
        $goods_list = $this->request->param('goods_list');
        $primary_address = $this->request->param('primary_address');
        $address = $this->request->param('address');
        $consignee = $this->request->param('consignee');
        $mobile = $this->request->param('mobile');
        $remark = $this->request->param('remark');
        $time = time();
        $sn = getOrderSn();
        $data = [
            'time' => $time,
            "goods_list"=> $goods_list,
            "primary_address"=> $primary_address,
            "address"=> $address,
            'third_sn' => $sn,
            'consignee' => $consignee,
            'mobile' => $mobile,
            'remark' => $remark,
        ];
        $sign = $this->getSignature($data,config('app.APIKEY'));
        $param = [
            "api_id"=> config('app.APIID'),
            "sign"=> $sign,
            "data"=> $data,
        ];

        $url = 'http://main.miniappss.com/terminal/api.order/placeOrder';

        $res = $this->curlPost($url,$param);
        if(isset($res['code']) && $res['code'] != 200){
            return $this->error([],$res['msg'],$res['error_code']);
        }
        return $this->success($res['data'],$res['msg']);
    }

    // 订单查询
    public function queryOrder()
    {
        $order_sn = $this->request->param('order_sn');
        $time = time();
        $data = [
            'time' => $time,
            "order_sn"=> $order_sn,
        ];
        $sign = $this->getSignature($data,config('app.APIKEY'));
        $param = [
            "api_id"=> config('app.APIID'),
            "sign"=> $sign,
            "data"=> $data,
        ];

        $url = 'http://main.miniappss.com/terminal/api.order/queryOrder';

        $res = $this->curlPost($url,$param);
        if(isset($res['code']) && $res['code'] != 200){
            return $this->error([],$res['msg'],$res['error_code']);
        }
        return $this->success($res['data'],$res['msg']);
    }

    // 取消订单
    public function cancelOrder()
    {
        $order_sn = $this->request->param('order_sn');
        $remark = $this->request->param('remark');
        $time = time();
        $data = [
            'time' => $time,
            "order_sn"=> $order_sn,
            "remark"=> $remark,
        ];
        $sign = $this->getSignature($data,config('app.APIKEY'));
        $param = [
            "api_id"=> config('app.APIID'),
            "sign"=> $sign,
            "data"=> $data,
        ];

        $url = 'http://main.miniappss.com/terminal/api.order/cancelOrder';

        $res = $this->curlPost($url,$param);
        if(isset($res['code']) && $res['code'] != 200){
            return $this->error([],$res['msg'],$res['error_code']);
        }
        return $this->success($res['data'],$res['msg']);
    }

    




}
